#ifndef INC_datafeedh
#define INC_datafeedh
/*---------------------------------------------------------------------------
 
  FILENAME:
        datafeed.h
 
  PURPOSE:
        Define the datafeed API.
 
  REVISION HISTORY:
        Date            Engineer        Revision        Remarks
        12/16/2009      M.S. Teel       0               Original
 
  NOTES:
        
 
  LICENSE:
        Copyright (c) 2009, Mark S. Teel (mark@teel.ws)
  
        This source code is released for free distribution under the terms 
        of the GNU General Public License.
  
----------------------------------------------------------------------------*/

//  ... includes
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <math.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <radtimeUtils.h>
#include <radsysutils.h>
#include <radsocket.h>

#define WVIEW_DATA_ONLY             TRUE
#include <datadefs.h>


//  ... macro definitions

// Define the floating point to fixed point conversion factor:
// (6 decimal places of accuracy):
#define DF_FP_SHIFT                 1000000


// The listen port for wview - clients connect here:
#define WV_DATAFEED_PORT            11011


//  ... typedefs

// Define the start frame sequences for loop and archive packets:
#define DF_START_FRAME_LENGTH       8
#define DF_LOOP_PKT_TYPE            1
#define DF_ARCHIVE_PKT_TYPE         2
#define DF_RQST_ARCHIVE_PKT_TYPE    3

// Define some times:
#define DF_WAIT_FIRST               500
#define DF_WAIT_MORE                250


#ifdef DATAFEED_INSTANTIATE
const USHORT DF_LOOP_START_FRAME[4] = 
{
    0xF388, 
    0xC6A2, 
    0xDADA, 
    0x0001
};
const USHORT DF_ARCHIVE_START_FRAME[4] = 
{
    0xF388, 
    0xC6A2, 
    0xDADA, 
    0x0002
};

// This one is sent by the client to request the next archive record after the
// dateTime submitted; allows the client to synchronize archive data:
const USHORT DF_RQST_ARCHIVE_START_FRAME[4] = 
{
    0xF388, 
    0xC6A2, 
    0xDADA, 
    0x0003
};
#else
extern const USHORT DF_LOOP_START_FRAME[4]; 
extern const USHORT DF_ARCHIVE_START_FRAME[4]; 
extern const USHORT DF_RQST_ARCHIVE_START_FRAME[4];
#endif


// LOOP_PKT_FP - define the LOOP (current readings) fixed point data format for 
//               datafeed transmission
typedef struct
{
    // minimum required fields - station must provide these:
    ULONG               barometer;              /* ! inches               */
    ULONG               stationPressure;        /* ! inches               */
    ULONG               altimeter;              /* ! inches               */
    ULONG               inTemp;                 /* ! degrees F            */
    ULONG               outTemp;                /* ! degrees F            */
    USHORT              inHumidity;             /* ! percent              */
    USHORT              outHumidity;            /* ! percent              */
    USHORT              windSpeed;              /* ! mph                  */
    USHORT              windDir;                /* ! degrees              */
    USHORT              windGust;               /* ! mph                  */
    USHORT              windGustDir;            /* ! degrees              */
    ULONG               rainRate;               /* ! in/hr                */
    ULONG               sampleRain;             /* ! inches               */
    ULONG               sampleET;               /* ! ET                   */
    USHORT              radiation;              /* ! watts/m^3            */
    ULONG               UV;                     /* ! UV index             */
    ULONG               dewpoint;               /* ! degrees F            */
    ULONG               windchill;              /* ! degrees F            */
    ULONG               heatindex;              /* ! degrees F            */

    // computed values - station should not alter these
    ULONG               stormRain;              /* inches                 */
    time_t              stormStart;             /* time_t                 */
    ULONG               dayRain;                /* inches                 */
    ULONG               monthRain;              /* inches                 */
    ULONG               yearRain;               /* inches                 */
    ULONG               dayET;                  /* inches                 */
    ULONG               monthET;                /* inches                 */
    ULONG               yearET;                 /* inches                 */
    ULONG               intervalAvgWCHILL;      /* degrees F              */
    short               intervalAvgWSPEED;      /* mph                    */
    USHORT              yearRainMonth;          /* 1-12 Rain Start Month  */

    // --- The following may or may not be supported for a given station ---

    // Vantage Pro
    USHORT              rxCheckPercent;         /* 0 - 100                */
    USHORT              tenMinuteAvgWindSpeed;  /* mph                    */
    USHORT              forecastIcon;           /* VP only                */
    USHORT              forecastRule;           /* VP only                */
    USHORT              txBatteryStatus;        /* VP only                */
    USHORT              consBatteryVoltage;     /* VP only                */
    short               extraTemp1;             /* degrees F              */
    short               extraTemp2;             /* degrees F              */
    short               extraTemp3;             /* degrees F              */
    short               soilTemp1;              /* degrees F              */
    short               soilTemp2;              /* degrees F              */
    short               soilTemp3;              /* degrees F              */
    short               soilTemp4;              /* degrees F              */
    short               leafTemp1;              /* degrees F              */
    short               leafTemp2;              /* degrees F              */
    UCHAR               extraHumid1;            /* percent                */
    UCHAR               extraHumid2;            /* percent                */
    UCHAR               soilMoist1;
    UCHAR               soilMoist2;
    UCHAR               leafWet1;
    UCHAR               leafWet2;

    // Vaisala WXT-510
    ULONG               wxt510Hail;             /* inches                 */
    ULONG               wxt510Hailrate;         /* in/hr                  */
    ULONG               wxt510HeatingTemp;      /* degrees F              */
    ULONG               wxt510HeatingVoltage;   /* volts                  */
    ULONG               wxt510SupplyVoltage;    /* volts                  */
    ULONG               wxt510ReferenceVoltage; /* volts                  */
    ULONG               wxt510RainDuration;     /* sec                    */
    ULONG               wxt510RainPeakRate;     /* volts                  */
    ULONG               wxt510HailDuration;     /* sec                    */
    ULONG               wxt510HailPeakRate;     /* volts                  */
    ULONG               wxt510Rain;             /* inches                 */

    // WMR918/968
    ULONG               wmr918Pool;             /* degrees F              */
    UCHAR               wmr918Humid3;           /* percent                */
    UCHAR               wmr918Tendency;         /* WMR's Forecast         */
    UCHAR               wmr918WindBatteryStatus;
    UCHAR               wmr918RainBatteryStatus;
    UCHAR               wmr918OutTempBatteryStatus;
    UCHAR               wmr918InTempBatteryStatus;
    UCHAR               wmr918poolTempBatteryStatus;
    UCHAR               wmr918extra1BatteryStatus;
    UCHAR               wmr918extra2BatteryStatus;
    UCHAR               wmr918extra3BatteryStatus;

}
LOOP_PKT_FP;

// ARCHIVE_PKT_FP - define the fixed point ARCHIVE data format for datafeeds
typedef struct
{
    time_t              dateTime;               /* record date and time        */
    long                usUnits;                /* US units (0 or 1)           */
    long                interval;               /* Archive Interval in minutes */
    ULONG               value[DATA_INDEX_MAX];
}
ARCHIVE_PKT_FP;


//  ... API prototypes

// Frame sync utility:
// Returns DF_LOOP_PKT_TYPE, DF_ARCHIVE_PKT_TYPE or DF_RQST_ARCHIVE_PKT_TYPE 
//   if a valid frame header of one of those types is received, FALSE if not a 
//   valid frame header and ERROR if there is a socket error:
extern int datafeedSyncStartOfFrame(RADSOCK_ID socket);


// LOOP_PKT byteorder and fixed point conversions:
extern int datafeedConvertLOOP_HTON(LOOP_PKT_FP* dest, LOOP_PKT* src);
extern int datafeedConvertLOOP_NTOH(LOOP_PKT* dest, LOOP_PKT_FP* src);

// ARCHIVE_PKT byteorder and fixed point conversions:
extern int datafeedConvertArchive_HTON(ARCHIVE_PKT_FP* dest, ARCHIVE_PKT* src);
extern int datafeedConvertArchive_NTOH(ARCHIVE_PKT* dest, ARCHIVE_PKT_FP* src);


#endif


